package Servlet.user;

import Dao.UserDao;
import Manage.User;
import Utils.mybatisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class usersServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        ServletContext application=request.getServletContext();
        String username=(String)application.getAttribute("username");
        SqlSession session=mybatisUtils.getSqlSession();
        UserDao dao= session.getMapper(UserDao.class);
        User user=new User();
        user=dao.Selectusers(username);
        PrintWriter pw=response.getWriter();
        //表格修饰样式
        pw.print("<link rel=\"stylesheet\" href=css/table.css type=\"text/css\">");
        if (null==user){
            pw.print("    <script type=\"text/javascript\" src=\"js/jquery-3.4.1.js\"></script>\n" +
                    "    <script>\n" +
                    "        //检查格式是否输入有误\n" +
                    "        lose=function(para) {\n" +
                    "            para.onblur = function () {\n" +
                    "                if (para.validity.patternMismatch)\n" +
                    "                    para.setCustomValidity(\"格式输入有误，请检查\");\n" +
                    "                else\n" +
                    "                    para.setCustomValidity(\"\");\n" +
                    "            }\n" +
                    "        }\n" +
                    "        $(document).ready(function () {\n" +
                    "            let phone=document.getElementById(\"pho\");\n" +
                    "            let num=document.getElementById(\"num\");\n" +
                    "            let cam=document.getElementById(\"cam\");\n" +
                    "            let nan=document.getElementById(\"nan\");\n" +
                    "            let lv=document.getElementById(\"nv\");\n" +
                    "            let sex=document.getElementById(\"td1\");\n" +
                    "            if (\"女\"===sex.innerText)\n" +
                    "                nv.setAttribute(\"checked\",\"true\");\n" +
                    "            else if (sex.innerText===\"男\")\n" +
                    "                nan.setAttribute(\"checked\",\"true\");\n" +
                    "            lose(phone);\n" +
                    "            lose(num);\n" +
                    "            lose(cam);\n" +
                    "        })\n" +
                    "    </script>\n" +
                    "<!--    普通用户-->\n" +
                    "    <form action=\"/user/insert\">\n" +
                    "        <table border=\"1px\" align=\"center\">\n" +
                    "            <tr>\n" +
                    "                <th>用户名</th>\n" +
                    "                <th>电话</th>\n" +
                    "                <th>性别</th>\n" +
                    "                <th>地址</th>\n" +
                    "                <th>拥有摄像头数量</th>\n" +
                    "                <th></th>\n" +
                    "            </tr>\n" +
                    "            <tr>\n" +
                    "                <td><input type=\"text\" name=\"username\" value="+username+" readonly></td>\n" +
                    "                <td>\n" +
                    "                    <input type=\"text\"  id=\"pho\" name=\"phone\" required pattern=\"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$\">\n" +
                    "                </td>\n" +
                    "                <td>" +
                    "                    男<input type=\"radio\" name=\"sex\" value=\"男\" id=\"nan\" checked>\n" +
                    "                    女<input name=\"sex\" type=\"radio\" value=\"女\" id=\"nv\">\n" +
                    "                </td>\n" +
                    "                <td><input type=\"text\"  name=\"address\" required></td>\n" +
                    "                <td><input type=\"text\"  name=\"num\" required id=\"num\" pattern=\"^[0-9]*$\"></td>\n" +
                    "                <td><input class=\"mybt\" type=\"submit\" value=\"保存\"></td>\n" +
                    "            </tr>\n" +
                    "        </table>\n" +
                    "    </form>\n" +
                    "    <form action=\"/user/caminsert\" >\n" +
                    "        <table border=\"1px\" align=\"center\">\n" +
                    "            <tr>\n" +
                    "                <th>摄像头所在街道</th>\n" +
                    "                <th hidden id=\"td1\"></th>\n" +
                    "            </tr>\n" +
                    "            <tr>\n" +
                    "                <td hidden><input type=\"text\" name=\"username\" value="+username+" ></td>\n" +
                    "                <td><input type=\"text\" name=\"street\" ></td>\n" +
                    "                <td><input class=\"mybt\" type=\"submit\" value=\"保存\"></td>\n" +
                    "            </tr>\n" +
                    "        </table>\n" +
                    "    </form>");
            }
        else{
            pw.print("    <script type=\"text/javascript\" src=\"js/jquery-3.4.1.js\"></script>\n" +
                    "    <script>\n" +
                    "        //检查格式是否输入有误\n" +
                    "        lose=function(para) {\n" +
                    "            para.onblur = function () {\n" +
                    "                if (para.validity.patternMismatch)\n" +
                    "                    para.setCustomValidity(\"格式输入有误，请检查\");\n" +
                    "                else\n" +
                    "                    para.setCustomValidity(\"\");\n" +
                    "            }\n" +
                    "        }\n" +
                    "        $(document).ready(function () {\n" +
                    "            let phone=document.getElementById(\"pho\");\n" +
                    "            let num=document.getElementById(\"num\");\n" +
                    "            let cam=document.getElementById(\"cam\");\n" +
                    "            let nan=document.getElementById(\"nan\");\n" +
                    "            let lv=document.getElementById(\"nv\");\n" +
                    "            let sex=document.getElementById(\"td1\");\n" +
                    "            if (\"女\"===sex.innerText)\n" +
                    "                nv.setAttribute(\"checked\",\"true\");\n" +
                    "            else if (sex.innerText===\"男\")\n" +
                    "                nan.setAttribute(\"checked\",\"true\");\n" +
                    "            lose(phone);\n" +
                    "            lose(num);\n" +
                    "            lose(cam);\n" +
                    "        })\n" +
                    "    </script>\n" +
                    "<!--    普通用户-->\n" +
                    "    <form action=\"/user/insert\">\n" +
                    "        <table border=\"1px\" align=\"center\">\n" +
                    "            <tr>\n" +
                    "                <th>用户名</th>\n" +
                    "                <th>电话</th>\n" +
                    "                <th>性别</th>\n" +
                    "                <th>地址</th>\n" +
                    "                <th>拥有摄像头数量</th>\n" +
                    "                <th></th>\n" +
                    "            </tr>\n" +
                    "            <tr>\n" +
                    "                <td><input type=\"text\" name=\"username\" value="+username+" readonly></td>\n" +
                    "                <td>\n" +
                    "                    <input type=\"text\" value="+user.getPhone()+" id=\"pho\" name=\"phone\" required pattern=\"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$\">\n" +
                    "                </td>\n" +
                    "                <td>" +
                    "                    男<input type=\"radio\" name=\"sex\" value=\"男\" id=\"nan\" checked>\n" +
                    "                    女<input name=\"sex\" type=\"radio\" value=\"女\" id=\"nv\">\n" +
                    "                </td>\n" +
                    "                <td><input type=\"text\" value="+user.getAddress()+" name=\"address\" required></td>\n" +
                    "                <td><input type=\"text\" value="+user.getNum()+" name=\"num\" required id=\"num\" pattern=\"^[0-9]*$\"></td>\n" +
                    "                <td><input class=\"mybt\" type=\"submit\" value=\"保存\"></td>\n" +
                    "            </tr>\n" +
                    "        </table>\n" +
                    "    </form>\n" +
                    "    <form action=\"/user/caminsert\" >\n" +
                    "        <table border=\"1px\" align=\"center\">\n" +
                    "            <tr>\n" +
                    "                <th>摄像头所在街道</th>\n" +
                    "                <th hidden id=\"td1\">"+user.getSex()+"</th>\n" +
                    "            </tr>\n" +
                    "            <tr>\n" +
                    "                <td hidden><input type=\"text\" name=\"username\" value="+username+" ></td>\n" +
                    "                <td><input type=\"text\" name=\"street\" ></td>\n" +
                    "                <td><input class=\"mybt\" type=\"submit\" value=\"保存\"></td>\n" +
                    "            </tr>\n" +
                    "        </table>\n" +
                    "    </form>");
        }


        pw.flush();
        pw.close();
        session.close();
    }
}
